from Attriabte import Color
from Entiy.Config import Config
from Entiy.WordGroup import WordGroup
from View.WindowComponent.BodyFrame.BodyFrame import BodyFrame
import customtkinter as ctk
import tkinter as tk


class Setting(BodyFrame):
    def __init__(self, layout, ws=False):
        super().__init__(layout, ws)
        self.config = Config()

    def loadConfig(self, option, slider):
        policy = self.config.getValue("WordConfig", 'policy')
        count = self.config.getValue("WordConfig", 'count')

        option.set(policy)
        slider.set(int(count if count != 'all' else len(WordGroup().get_all_word())))

    def setOptions(self, options):
        value = options.get()
        self.config.setDas("WordConfig", "policy", value)

    def setValue(self, slider, label):
        value = int(slider.get())
        if len(WordGroup().get_all_word()) == value:
            label['text'] = f"抽取个数(全部)"
            self.config.setDas("WordConfig", "count", "all")
        else:
            label['text'] = f"抽取个数({value})"
            self.config.setDas("WordConfig", "count", str(value))

    def pack(self):
        self.basicTheme('设置')

        tk.Label(
            self.inner_frame,
            text='出题策略',
            font=('黑体',12),
            **self.attribate_basic
        ).pack(pady=12)

        options = ctk.CTkOptionMenu(
            self.inner_frame,
            text_font=("黑体", 15),
            values=["顺序且全部","打乱"],
            command=lambda e:self.setOptions(options)
        )
        options.pack(pady=12)

        label1 = tk.Label(
            self.inner_frame,
            text=f'抽取个数({self.config.getValue("WordConfig","count")})',
            font=('黑体',12),
            **self.attribate_basic
        )
        label1.pack(pady=12)

        slider = ctk.CTkSlider(
            self.inner_frame,
            from_=1,
            to=len(WordGroup().get_all_word()),
            command=lambda e:self.setValue(slider, label1)
        )
        slider.pack(pady=6)

        self.loadConfig(options, slider)
